package com.trailwalker.javafx.widget.dialog;

import com.trailwalker.JavaUtils.string.StringUtils;
import com.trailwalker.javafx.utils.image.ImageUtils;
import com.trailwalker.javafx.utils.resource.ResourceUtils;
import com.trailwalker.javafx.utils.style.AdaptUtils;
import com.trailwalker.javafx.utils.style.StyleUtils;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.util.List;

/**
 * @author Trailwalker
 * @create 2021-07-12 21:06
 * @des
 */
public class CustomDialog extends BaseDialog {

    private Stage mDialogStage;
    private CustomDialogConfig mCustomDialogConfig;
    private AdaptUtils mAdaptUtils;
    private Label lblTitle;
    private ImageView ivClose;

    private CustomDialog(CustomDialogConfig customDialogConfig) {
        mCustomDialogConfig = customDialogConfig;
        onInit();
    }

    private void onInit() {
        initLoad();
        initView();
        initData();
        initListener();
        onReady();
    }

    private void onReady() {

    }

    private void initListener() {
        if (ivClose != null) {
            ivClose.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    if (event.getButton().equals(MouseButton.PRIMARY)) {
                        dismiss();
                        if (mCustomDialogConfig.onDialogDismissListener != null) {
                            mCustomDialogConfig.onDialogDismissListener.onDismiss();
                        }
                    }
                }
            });
        }
    }

    private void initData() {
        if (lblTitle != null) {
            if (StringUtils.isValid(mCustomDialogConfig.titleText)) {
                lblTitle.setText(mCustomDialogConfig.titleText);
            }
        }
    }

    private void initView() {
        double primaryStageWidth = mCustomDialogConfig.primaryStage.getWidth();

        mDialogStage = new Stage();
        if (mCustomDialogConfig.dialogHeight != null) {
            mDialogStage.setHeight(mCustomDialogConfig.dialogHeight);
        }
        double dialogStageWidth;
        if (mCustomDialogConfig.dialogWidth != null) {
            dialogStageWidth = mCustomDialogConfig.dialogWidth;
        } else {
            dialogStageWidth = primaryStageWidth / 4;
        }
        mDialogStage.setWidth(dialogStageWidth);
        //窗口设置
        mDialogStage.initOwner(mCustomDialogConfig.primaryStage);
        mDialogStage.initStyle(StageStyle.TRANSPARENT);
        mDialogStage.initModality(Modality.WINDOW_MODAL);

        AnchorPane apRoot = new AnchorPane();

        Scene scene = new Scene(apRoot);
        mDialogStage.setScene(scene);
        if (mCustomDialogConfig.stylesheets != null) {
            scene.getStylesheets().addAll(mCustomDialogConfig.stylesheets);
        }

        BorderPane bpRoot = new BorderPane();
        apRoot.getChildren().add(bpRoot);
        mAdaptUtils.setMatchParentAnchorPane(bpRoot);

        initTitleView(bpRoot);

        initContentView(bpRoot);
    }

    private void initContentView(BorderPane bpRoot) {
        if (mCustomDialogConfig.contentView != null) {
            bpRoot.setCenter(mCustomDialogConfig.contentView);
        }
    }

    private void initTitleView(BorderPane bpRoot) {
        if (mCustomDialogConfig.titleView != null) {
            bpRoot.setTop(mCustomDialogConfig.titleView);
        } else {
            AnchorPane apTitle = new AnchorPane();
            bpRoot.setTop(apTitle);
            mAdaptUtils.setScaleHeight(apTitle, 40);
            if (StringUtils.isValid(mCustomDialogConfig.primaryColor)) {
                StyleUtils.setBackgroundColor(apTitle, mCustomDialogConfig.primaryColor);
            }

            StackPane spTitle = new StackPane();
            apTitle.getChildren().add(spTitle);
            mAdaptUtils.setMatchParentAnchorPane(spTitle);
            mAdaptUtils.setScalePaddingLeftAndRight(spTitle, 10);
            spTitle.setAlignment(Pos.CENTER);

            lblTitle = new Label();
            spTitle.getChildren().add(lblTitle);
            if (mCustomDialogConfig.titleTextPos != null) {
                StackPane.setAlignment(lblTitle, mCustomDialogConfig.titleTextPos);
            } else {
                StackPane.setAlignment(lblTitle, Pos.CENTER_LEFT);
            }
            mAdaptUtils.setScaleTextSize(lblTitle, 20);
            if (StringUtils.isValid(mCustomDialogConfig.primaryColor)) {
                StyleUtils.setTextColor(lblTitle, "#ffffff");
            }

            ivClose = ImageUtils.getImageView(this, 15, "close_normal.png", "close_press.png");
            spTitle.getChildren().add(ivClose);
            StackPane.setAlignment(ivClose, Pos.CENTER_RIGHT);
            ivClose.setCursor(Cursor.HAND);
        }
        setMoveAble(mDialogStage, bpRoot.getTop());
    }

    private void initLoad() {
        mAdaptUtils = AdaptUtils.getInstance();
    }

    public void show() {
        double primaryStageHeight = mCustomDialogConfig.primaryStage.getHeight();
        double primaryStageWidth = mCustomDialogConfig.primaryStage.getWidth();
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                System.out.println("mDialogStage.getWidth() >> " + mDialogStage.getWidth());
                System.out.println("mDialogStage.getHeight() >> " + mDialogStage.getHeight());
                mDialogStage.setX(mCustomDialogConfig.primaryStage.getX() + primaryStageWidth / 2 - mDialogStage.getWidth() / 2);
                mDialogStage.setY(mCustomDialogConfig.primaryStage.getY() + primaryStageHeight / 2 - mDialogStage.getHeight() / 2);
            }
        });
        mDialogStage.showAndWait();
    }

    public void dismiss() {
        mDialogStage.close();
    }

    public static class CustomDialogBuilder {

        private CustomDialogConfig mCustomDialogConfig;

        public CustomDialogBuilder(Stage primaryStage) {
            mCustomDialogConfig = new CustomDialogConfig();
            mCustomDialogConfig.primaryStage = primaryStage;
        }

        public CustomDialogBuilder setStylesheets(List<String> stylesheets) {
            mCustomDialogConfig.stylesheets = stylesheets;
            return this;
        }

        public CustomDialogBuilder setDialogWidth(double dialogWidth) {
            mCustomDialogConfig.dialogWidth = dialogWidth;
            return this;
        }

        public CustomDialogBuilder setDialogHeight(double dialogHeight) {
            mCustomDialogConfig.dialogHeight = dialogHeight;
            return this;
        }

        public CustomDialogBuilder setTitleView(Region titleView) {
            mCustomDialogConfig.titleView = titleView;
            return this;
        }

        public CustomDialogBuilder setContentView(Region contentView) {
            mCustomDialogConfig.contentView = contentView;
            return this;
        }

        public CustomDialogBuilder setTitleText(String titleText) {
            mCustomDialogConfig.titleText = titleText;
            return this;
        }

        public CustomDialogBuilder setTitleTextPos(Pos titleTextPos) {
            mCustomDialogConfig.titleTextPos = titleTextPos;
            return this;
        }

        public CustomDialogBuilder setPrimaryColor(String primaryColor) {
            mCustomDialogConfig.primaryColor = primaryColor;
            return this;
        }

        public CustomDialogBuilder setOnDialogDismissListener(OnDialogDismissListener onDialogDismissListener) {
            mCustomDialogConfig.onDialogDismissListener = onDialogDismissListener;
            return this;
        }

        public CustomDialog build() {
            return new CustomDialog(mCustomDialogConfig);
        }
    }

    static class CustomDialogConfig {
        Stage primaryStage;
        List<String> stylesheets;
        Double dialogWidth;
        Double dialogHeight;
        Region titleView;
        Region contentView;
        String titleText;
        Pos titleTextPos;
        String primaryColor;
        OnDialogDismissListener onDialogDismissListener;
    }

    public interface OnDialogDismissListener {
        void onDismiss();
    }

}
